﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Extensions;

namespace Boggle
{
    public class BoggleBoard
    {
        private readonly List<List<BoggleCell>> _grid;
        public int Width { private set; get; }
        public int Height { private set;  get; }
        public BoggleCell this [int row, int column]
        {
            get { return _grid[row][column]; }
        }

        public BoggleBoard(int height, int width, IEnumerable<string> letters)
        {
            if (letters == null)
            {
                throw new ArgumentNullException("letters");
            }
            if (!letters.Any())
            {
                throw new ArgumentException("letters cannot be empty");
            }
            if (height < 1)
            {
                throw new ArgumentException("height must be greater than 0");
            }
            if (width < 1)
            {
                throw new ArgumentException("width must be greater than 0");
            }
            if (height * width != letters.Count())
            {
                throw new ArgumentException("letters do not match the dimension of the boggle board");
            }


            //save dimenensions
            Height = height;
            Width = width;

            _grid = new List<List<BoggleCell>>();
            letters.Each((letter, index) =>
                {
                    int column = index%width;
                    //create a new row if its the first column
                    if (column == 0)
                    {
                        List<BoggleCell> newRow = new List<BoggleCell>();
                        _grid.Add(newRow);
                    }

                    int row = _grid.Count - 1;
                    //add new cell to last row in grid
                    BoggleCell newCell = new BoggleCell(letter, row, column);
                    _grid.Last().Add(newCell);
                });
        }

        public IEnumerable<BoggleCell> GetNeighbors(int row, int column)
        {
            ICollection<BoggleCell> neighbors = new LinkedList<BoggleCell>();
            bool isFirstRow = row == 0;
            bool isLastRow = row == this.Height - 1;

            bool isFirstColumn = column == 0;
            bool isLastColumn = column == this.Width - 1;

            //top left diagnol
            if (!isFirstRow && !isFirstColumn)
            {
                neighbors.Add(this[row - 1, column - 1]);
            }
            //top 
            if (!isFirstRow)
            {
                neighbors.Add(this[row -1, column]);
            }
            //top right diagnol
            if (!isFirstRow && !isLastColumn)
            {
                neighbors.Add(this[row -1, column + 1]);
            }
            //right
            if (!isLastColumn)
            {
                neighbors.Add(this[row, column + 1]);
            }
            //bottom right diagnlol
            if(!isLastRow && !isLastColumn)
            {
                neighbors.Add(this[row +1, column + 1]);
            }
            //bottom
            if(!isLastRow)
            {
                neighbors.Add(this[row +1, column]);
            }
            //bottom left diagnol
            if(!isLastRow && !isFirstColumn)
            {
                neighbors.Add(this[row + 1, column - 1]);
            }
            //left
            if(!isFirstColumn)
            {
                neighbors.Add(this[row, column - 1]);
            }
            return neighbors;
        }
    }
}
